package com.able.concurrency.t1;

import java.util.concurrent.TimeUnit;

/**
 * @author abel.he
 * @data 2018/8/15 19:50
 * @describe 所对象之间的变更问题
 * 同步代码一旦加锁后，那么会有一个临时的锁引用执行所对象，和真实的引用无直接关联
 * 在锁未释放之前，修改锁对象引用，不会影响同步代码的执行
 */
public class Test13 {

    private Object o = new Object();

    void m1() {
        System.out.println(Thread.currentThread().getName() + " start");
        synchronized (o) {
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "-" + o);
        }
    }

    public static void main(String[] args) {
        final Test13 test13 = new Test13();
        new Thread(new Runnable() {
            public void run() {
                test13.m1();
            }
        },"t1").start();

        Thread thread1 = new Thread(new Runnable() {
            public void run() {
                test13.m1();
            }
        }, "t2");

        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        test13.o = new Object();
        thread1.start();
    }
}
